package service

import (
	"encoding/json"
	"errors"
	"haiqiu-go-admin/dao"
	"haiqiu-go-admin/entity"
	"haiqiu-go-admin/entity/dto"
	"haiqiu-go-admin/entity/vo"
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/utils"
	"log"
	"net/http"
	"strconv"
	"time"
)

func DeptTree(w http.ResponseWriter, r *http.Request) (treeData any, err error) {
	formatJson := utils.GetParamsFormatJson(w, r)
	var deptQueryDto dto.DeptQueryDto
	if formatJson["deptName"] != nil {
		var s = formatJson["deptName"][0]
		deptQueryDto.Params.DeptName = &s
	}
	if formatJson["status"] != nil {
		var s = formatJson["status"][0]
		boolValue, err := strconv.ParseBool(s)
		if err != nil {
			panic(err)
		}
		deptQueryDto.Params.Status = &boolValue
	}
	depts, err := dao.DeptList(deptQueryDto)
	if err != nil {
		return nil, err
	}
	vos := make([]vo.DeptTreeVo, 0)
	for _, v := range *depts {
		var data vo.DeptTreeVo
		data.Id = v.Id
		data.DeptName = v.DeptName
		data.Cover = v.Cover
		data.Leader = v.Leader
		data.Phone = v.Phone
		data.Email = v.Email
		data.ParentId = v.ParentId
		data.Desc = v.Desc
		data.Sort = v.Sort
		data.Status = v.Status
		data.IsDelete = v.IsDelete
		data.CreateTime = v.CreateTime
		data.UpdateTime = v.UpdateTime
		vos = append(vos, data)
	}
	if err != nil {
		log.Println(err.Error())
		return nil, err
	}
	return deptTreeDataList(vos), nil
}

func deptTreeDataList(vos []vo.DeptTreeVo) *[]vo.DeptTreeVo {
	finalDeptVos := make([]vo.DeptTreeVo, 0)
	for _, dept := range vos {
		if dept.ParentId == 0 {
			finalDeptVos = append(finalDeptVos, deptChildrenList(dept, vos))
		}
	}
	return &finalDeptVos
}

func deptChildrenList(dept vo.DeptTreeVo, vos []vo.DeptTreeVo) vo.DeptTreeVo {
	dept.Children = make([]vo.DeptTreeVo, 0)
	for _, child := range vos {
		if child.ParentId == dept.Id {
			dept.Children = append(dept.Children, deptChildrenList(child, vos))
		}
	}
	return dept
}

// DeptById 根据ID查询数据
func DeptById(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson := utils.GetParamsFormatJson(w, r)
	deptIdStr := formatJson["id"][0]
	deptId, err := strconv.ParseInt(deptIdStr, 10, 64)
	if err != nil {
		panic(err)
		return nil, err
	}
	dept, err := dao.DeptById(deptId)
	if err != nil {
		return nil, err
	}
	return &dept, nil
}

// DeptPage 分页查询
func DeptPage(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, errors.New(result.ParamsErr)
	}
	var page = dto.PageDto{}
	err = json.Unmarshal(formatJson, &page)
	if err != nil {
		log.Println(err)
	}
	count := dao.DeptPageCount()
	if count == 0 {
		return vo.PageData{Total: 0}, nil
	}
	var pageData vo.PageData
	list, err := dao.DeptPageList((page.PageIndex-1)*page.PageSize, page.PageSize)
	pageData.Data = list
	pageData.Total = count
	pageData.PageIndex = page.PageIndex
	pageData.PageSize = page.PageSize
	if err != nil {
		return nil, err
	}
	return pageData, nil
}

// DeptAdd 新增数据
func DeptAdd(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, err
	}
	var sysDept = entity.SysDept{}
	err = json.Unmarshal(formatJson, &sysDept)
	if err != nil {
		return nil, err
	}
	var nowTime = time.Now()
	sysDept.CreateTime = &nowTime
	sysDept.UpdateTime = &nowTime
	add, err := dao.DeptAdd(&sysDept)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// DeptUpdate 根据ID更新数据
func DeptUpdate(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, err
	}
	var sysDept = entity.SysDept{}
	err = json.Unmarshal(formatJson, &sysDept)
	if err != nil {
		return nil, err
	}
	var nowTime = time.Now()
	sysDept.UpdateTime = &nowTime
	add, err := dao.DeptUpdate(&sysDept)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// DeptDeleteBatch 批量删除数据
func DeptDeleteBatch(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, err
	}
	var ids []int64
	err = json.Unmarshal(formatJson, &ids)
	if err != nil {
		return nil, err
	}

	add, err := dao.DeptDeleteBatch(ids)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// DeptList 查询所有数据
func DeptList(w http.ResponseWriter, r *http.Request) (*[]entity.SysDept, error) {
	formatJson := utils.GetParamsFormatJson(w, r)
	var deptQueryDto dto.DeptQueryDto
	if formatJson["deptName"] != nil {
		var s = formatJson["deptName"][0]
		deptQueryDto.Params.DeptName = &s
	}
	if formatJson["status"] != nil {
		var s = formatJson["status"][0]
		boolValue, err := strconv.ParseBool(s)
		if err != nil {
			panic(err)
		}
		deptQueryDto.Params.Status = &boolValue
	}
	deptList, err := dao.DeptList(deptQueryDto)
	if err != nil {
		log.Println(err.Error())
		return nil, err
	}
	return deptList, nil
}

func ActiveDept(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		log.Println(err.Error())
		panic(err)
		return nil, err
	}
	var sysDept []entity.SysDept
	err = json.Unmarshal(formatJson, &sysDept)
	if err != nil {
		log.Println(err.Error())
		panic(err)
		return nil, err
	}
	rowsAffected, err := dao.ActiveDeptById(sysDept)
	return rowsAffected, err
}
